package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.project.dto.RiderDto;
import com.project.dto.riderToBack.OrderDtoFroBack;
import com.project.dto.userToMerchant.OrderDto;
import com.project.dto.userToMerchant.OrderInfoDto;
import com.project.entity.Detail;
import com.project.entity.Order;
import com.project.entity.Rider;
import com.project.entity.WithDraw;
import com.project.mapper.IWithDrawMapper;
import com.project.mapper.TDetailMapper;
import com.project.mapper.TOrderMapper;
import com.project.mapper.TRiderMapper;
import com.rabbitmq.client.Channel;
import lombok.With;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class MQService {

    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    private TOrderMapper mapper;
    @Autowired
    private TRiderMapper riderMapper;
    @Autowired
    private IWithDrawMapper withDrawMapper;
    @Autowired
    private TDetailMapper detailMapper;
    //监听用户订单
    @RabbitListener(queues = "UserToBackQueue")
    public void getMessage(OrderDto orderDto, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag){
        System.out.println("=============================================="+orderDto);
        System.out.println("11111111111111111111111111"+orderDto.getInfoList());
        Order order = new Order();
        order.setNumber(orderDto.getOrderCode());
        order.setAllMoney(Double.parseDouble(orderDto.getOrderMoney()));
        order.setFinishTime(orderDto.getReceiveTime());
        order.setBusinessIncome(Double.parseDouble(orderDto.getOrderMoney())*0.8);
        order.setRiderIncome(2);
        order.setAddress(orderDto.getReceiverAddress());
        order.setPhone(orderDto.getReceiverPhone());
        order.setUsername(orderDto.getReceiver());
        order.setCustomerId(orderDto.getUserCode());
        order.setBusinessId(orderDto.getShopCode());
        order.setRiderId(orderDto.getRiderCode());
        mapper.insert(order);
        //订单详情
        Integer orderId = order.getOrderId();
        List<OrderInfoDto> infoList = orderDto.getInfoList();
        List<Detail> list = new ArrayList<>();
        for (int i = 0; i < infoList.size(); i++) {
            OrderInfoDto orderInfoDto = infoList.get(i);
            Detail detail = new Detail();
            detail.setName(orderInfoDto.getProductName());
            detail.setNum(orderInfoDto.getBuyNum());
            detail.setOrderId(orderId);
            detail.setPrice(orderInfoDto.getProductPrice());
            list.add(detail);
        }
        //添加订单详情
        for (Detail detail : list) {
            detailMapper.insert(detail);
        }
    }

    //监听骑手提现需求
    @RabbitListener(queues = "RiderSendGetMoneyToBack")
    public void riderMoney(RiderDto riderDto,Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws Exception{
        DateTimeFormatter dfDate = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        WithDraw withDraw = new WithDraw();
        withDraw.setRiderCode(riderDto.getCode());
        withDraw.setMoney(riderDto.getRiderGetMoney());
        withDraw.setStartTime(dfDate.format(LocalDateTime.now()));
        withDrawMapper.insert(withDraw);
    }

    //监听骑手修改
    @RabbitListener(queues = "RiderUpdateRiderInfoToBack")
    public void riderUpdate(RiderDto riderDto,Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag){

        System.out.println("22222222222222222");
        Rider rider = new Rider();
        UpdateWrapper<Rider> wrapper = new UpdateWrapper<>();
        wrapper.eq("r_code",riderDto.getCode());
        rider.setPhone(riderDto.getTel());
        riderMapper.update(rider,wrapper);
    }
}
